package cuit.game.controller;


import cuit.game.entity.Application;
import cuit.game.entity.Contest;
import cuit.game.entity.User;
import cuit.game.service.ApplicationService;
import cuit.game.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //注入类
    @Autowired
    private UserService userService;

    @Autowired
    private ApplicationService applicationService;

    //用户信息展示
    @GetMapping("/oneself")
    public String viewUser(HttpServletRequest request, Model model) {

        // 获取HttpSession对象
        HttpSession session = request.getSession();

        // 将用户ID保存到会话中
        User user = (User)session.getAttribute("user");
        model.addAttribute("user", user);

        return "user/oneself";
    }

    //修改用户信息
    @PostMapping("/update")
    public String updateUser(@RequestParam("username") String name,
                             @RequestParam("pwd") String pwd,
                             @RequestParam("email") String email, HttpServletRequest request,Model model) {



        // 获取HttpSession对象
        HttpSession session = request.getSession();

        // 获取用户
        User user = (User)session.getAttribute("user");

        user.setUsername(name);
        user.setEmail(email);
        user.setPwd(pwd);
        userService.save(user);

        //更新session中的数据
        session.setAttribute("user", user);
        model.addAttribute("user", user);

        return "user/oneself";

    }

}
